<template>
	<view class="page">
		<div style="display: flex;justify-content: center;margin-bottom: 60px;">
			<image src="../../static/appicon.png" mode="" style="height: 35px;object-fit: contain;"></image>
		</div>
		<button type="primary" open-type="getPhoneNumber" @getphonenumber="getphonenumber"
			style="width: 100%;font-size: 16px;">微信授权登录</button>
		<button type="primary" open-type="getPhoneNumber" @getphonenumber="getphonenumber"
			style="width: 100%;margin-top: 20px;font-size: 16px;">手机号登录</button>
	</view>
</template>

<script>
	import {
		Login
	} from "../../api/login.js"
	export default {
		data() {
			return {
				code: "",
				req: {}
			}
		},
		created() {
			console.log("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
			this.getWXCode()
		},
		methods: {
			getphonenumber(e) {
				let self = this;
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					let {
						detail
					} = e;
					self.req = detail
					self.req.code = this.code
					self.netLogin()
				} else {

				}
			},
			//先获取code
			getWXCode() {
				let self = this
				wx.login({
					success: (res) => {
						if (res.code) {
							let code = res.code;
							self.code = code
							uni.setStorageSync("wxcode",code)
						} else {

						}
					}
				})
			},
			netLogin() {
				Login(this.req).then(res => {
					let {
						code,
						data,
						msg
					} = res;
					if (code == 200) {
						let {
							token,
							tokenKey,
							openid
						} = data
						uni.setStorageSync("token", token)
						uni.setStorageSync("tokenKey", tokenKey)
						uni.setStorageSync("openid", openid)
						uni.redirectTo({
							url:"../kill-buy/index/index"
						})
					} else {
						uni.showToast({
							title: msg,
							icon: 'none'
						})
					}
				}).catch(e => {
					uni.showToast({
						title: JSON.stringify(e),
						icon: 'none'
					})

				})
			},

		}
	}
</script>

<style scoped>
	.page {
		flex: 1;
		display: flex;
		flex-direction: column;
		padding: 12px;
	}
</style>
